﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OracleClient;

public partial class Services_getModelParam : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

        string AgriParam = "";
        string RsIndex = "";
        string ImageID = "";
        string DistrictId = "";
        string SamplingDate = "";

        string calculateType = "";//11-14日添加，计算模型的类型

        //11-5日添加,查询数据库的表的名称---->
        string param_tbname = "";
        string rs_tbname = "";
        //<----11-5日添加

        string resultXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
        resultXml += "<ParamList>" + "\n";

        ImageID = Request.QueryString[0].ToString().Trim();
        AgriParam = Request.QueryString[1].ToString().Trim();
        RsIndex = Request.QueryString[2].ToString().Trim();

        calculateType = Request.QueryString[4].ToString().Trim();

        //11-5日下午添加--->
        if (RsIndex=="G")
        {
            RsIndex += "_" + Request.QueryString[3].ToString().Trim();
        }
        //<----11-5日下午添加
        //11-5日添加,查询数据库的表的名称---->
        //if (AgriParam.Trim() == "YEILD")
        //{
        //    param_tbname = "rs.sp_yeild_param";
        //    rs_tbname = "rs.sp_rsindex";
        //}
        //else
        //{
        //    param_tbname = "rs.sp_agri_param";
        //    rs_tbname = "rs.sp_rsindex";
        //}
        //<----11-5日添加

        if (calculateType == "grow")//长势
        {
            param_tbname = "rs.sp_agri_param";
            rs_tbname = "rs.sp_rsindex";
        }
        if (calculateType=="rs")//单产
        {
            param_tbname = "rs.sp_yeild_param";
            rs_tbname = "rs.sp_rsindex";
        }
        if (calculateType=="quality")//品质
        {
            param_tbname = "rs.sp_agri_param";
            rs_tbname = "rs.sp_rsindex";
        }
        if(calculateType=="Rsquality")
        {
            param_tbname = "rs.sp_rsindex";
            rs_tbname = "rs.sp_quality_param";
        }

        string queryString = "select DISTID,IMAGEDATE from RS.DISTRICT_IMAGE_SP_REF where IMAGEID = '" + ImageID + "'";
        OracleDataReader myReader = OracleDataBase.ExecuteReader(queryString);
        if (myReader.Read())
        {
            DistrictId = myReader[0].ToString();
            SamplingDate = Convert.ToDateTime(myReader[1]).ToShortDateString();
        }

        double[] ModelParam;
        try
        {
            //AgriRSModel myModel = new AgriRSModel(AgriParam, RsIndex, SamplingDate, DistrictId);//当建立单产模型时，出错，应该把构造函数改为带表名的版本
            AgriRSModel myModel = new AgriRSModel(AgriParam, RsIndex, SamplingDate, DistrictId, param_tbname, rs_tbname);
            if (calculateType == "quality")
            {
                ModelParam = myModel.createLinerModel(myModel.AgriParamDataset, myModel.RSIndexDataset);
                resultXml += "<Param a = '" + ModelParam[0] + "' b = '" + ModelParam[1] + "' r = '" + ModelParam[2] + "' />" + "\n";
                ModelParam = myModel.createExpModel(myModel.AgriParamDataset, myModel.RSIndexDataset);
                resultXml += "<Param a = '" + ModelParam[0] + "' b = '" + ModelParam[1] + "' r = '" + ModelParam[2] + "' />" + "\n";
                ModelParam = myModel.createLogModel(myModel.AgriParamDataset, myModel.RSIndexDataset);
                resultXml += "<Param a = '" + ModelParam[0] + "' b = '" + ModelParam[1] + "' r = '" + ModelParam[2] + "' />" + "\n";
            }else if(calculateType=="Rsquality")
            {
                ModelParam = myModel.createLinerModel( myModel.QualityDataset,myModel.RSIndexDataset);
                resultXml += "<Param a = '" + ModelParam[0] + "' b = '" + ModelParam[1] + "' r = '" + ModelParam[2] + "' />" + "\n";
                ModelParam = myModel.createExpModel(myModel.QualityDataset, myModel.RSIndexDataset);
                resultXml += "<Param a = '" + ModelParam[0] + "' b = '" + ModelParam[1] + "' r = '" + ModelParam[2] + "' />" + "\n";
                ModelParam = myModel.createLogModel(myModel.QualityDataset, myModel.RSIndexDataset);
                resultXml += "<Param a = '" + ModelParam[0] + "' b = '" + ModelParam[1] + "' r = '" + ModelParam[2] + "' />" + "\n";
            }
            else
            {
                ModelParam = myModel.createLinerModel(myModel.AgriParamDataset, myModel.RSIndexDataset);
                resultXml += "<Param a = '" + ModelParam[0] + "' b = '" + ModelParam[1] + "' r = '" + ModelParam[2] + "' />" + "\n";
                ModelParam = myModel.createExpModel(myModel.AgriParamDataset, myModel.RSIndexDataset);
                resultXml += "<Param a = '" + ModelParam[0] + "' b = '" + ModelParam[1] + "' r = '" + ModelParam[2] + "' />" + "\n";
                ModelParam = myModel.createLogModel(myModel.AgriParamDataset, myModel.RSIndexDataset);
                resultXml += "<Param a = '" + ModelParam[0] + "' b = '" + ModelParam[1] + "' r = '" + ModelParam[2] + "' />" + "\n";
            }
            

            resultXml += "</ParamList>" + "\n";

            Response.Write(resultXml);
        }
        catch (Exception ex)
        {
            resultXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
            resultXml += "<ParamList>" + "\n";
            resultXml += "</ParamList>" + "\n";
            Response.Write(resultXml);
            throw ex;
        }
    }
}
